package com.cty.controller;

import com.cty.pojo.ShopDetailVO;
import com.cty.service.ShopService;
import com.cty.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ShopController {

    @Autowired
    private ShopService shopService;

    @GetMapping("shop/kw/{keyword}/page/{pageNum}") //shop/kw/huoguo/page/1

    public ModelAndView getByKeywordAndPage(@PathVariable("keyword") String keyword,
                                            @PathVariable("pageNum")Integer pageNum){
        ServerResult serverResult = shopService.getByKeyword(keyword,pageNum);
        ModelAndView mav = new ModelAndView();

        if(serverResult.getCode() == 200){
            mav.addObject("kw",keyword);
            mav.addObject("pageInfo",serverResult.getData());
            System.out.println(serverResult.getData());
        }else{
            mav.addObject("dataInfo",serverResult.getData());
        }
        mav.setViewName("shop/shop_list");
        return mav;
    }

    @GetMapping("shop/{id}")
    public ModelAndView getById(@PathVariable("id") Integer id){
        ServerResult result = shopService.getById(id);
        ModelAndView mav = new ModelAndView();
        ShopDetailVO vo = (ShopDetailVO)result.getData();
        System.out.println("vo:" + vo);
        mav.addObject("vo",vo);
        mav.setViewName("/shop/shop_detail");

        return mav;
    }

    @GetMapping("shop/{id}/review")
    public ModelAndView getByIdForAddReview(@PathVariable("id") Integer id){
        ServerResult result = shopService.getByIdForAddReiew(id);
        ModelAndView mav = new ModelAndView();
        ShopDetailVO vo = (ShopDetailVO)result.getData();
        mav.addObject("vo",vo);
        mav.setViewName("/review/save_review");

        return mav;
    }
}
